package com.hschinese.pronunciation.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.widget.LinearLayout;

import com.hschinese.pronunciation.R;
import com.hschinese.pronunciation.utils.FileUtils;
import com.hschinese.pronunciation.utils.ParseTxtDataUtil;
import com.hschinese.pronunciation.utils.SharedPreferenceUtils;
import com.jaeger.library.StatusBarUtil;

import butterknife.Bind;
import butterknife.ButterKnife;
import studychinese.activity.HomeActivity;
import studychinese.activity.SetLangue;
import studychinese.utils.AppUtils;


public class StartPageActivity extends BaseActivity {
    @Bind(R.id.ll_back)
    LinearLayout llBack;
    private Context context = null;
    private SharedPreferenceUtils mSharedPrefUtil;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start_page);
        ButterKnife.bind(this);
        context = StartPageActivity.this;
        mSharedPrefUtil = SharedPreferenceUtils.getInstance(StartPageActivity.this);
        delayDestory();
        // TODO 做SharedPreference中的状态检查，看是否是第一次登陆
        if (SharedPreferenceUtils.getInstance(getApplicationContext()).getFirstLoginFlag()) {
            initData();
            SharedPreferenceUtils.getInstance(getApplicationContext()).setFirstLoginFlag();
        }
    }

    @Override
    protected void setStatusBar() {
        StatusBarUtil.setTranslucentForImageView(this, 0, llBack);
    }

    private void delayDestory() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {

                if (!AppUtils.isLogin()) {
                    startActivity(new Intent(StartPageActivity.this, SetLangue.class));
                    initData();
                } else {
                    startActivity(new Intent(StartPageActivity.this, HomeActivity.class));

                }

                ((Activity) context).finish();
            }
        }, 2000);
    }

    private void initData() {
        Thread thread = new Thread("parse data") {
            @Override
            public void run() {
                ParseTxtDataUtil.getInstance(getApplicationContext());
                FileUtils.initFolders();
                super.run();
            }
        };
        thread.start();
    }
}